package com.wzdigit.wms.wms.service.core;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.wms.basic.domain.transfer.GdnDtl;
import com.wzdigit.wms.basic.domain.transfer.GdnDtlGoods;
import com.wzdigit.wms.basic.domain.transfer.GdnHeader;
import com.wzdigit.wms.basic.client.request.gdn.other.SearchOrderGoodsRequest;
import com.wzdigit.wms.basic.client.request.gdn.other.SearchOtherOutOrderRequest;
import com.wzdigit.wms.basic.client.response.gdn.other.OtherOutOrderGoodsResponse;
import com.wzdigit.wms.basic.client.response.gdn.other.OtherOutOrderInfoResponse;
import com.wzdigit.wms.basic.mapper.core.order.manager.GdnManagerMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnDtlGoodsMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnHeaderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class GdnManagerService {

    @Autowired
    private GdnManagerMapper gdnManagerMapper;

    @Autowired
    private GdnHeaderMapper gdnHeaderMapper;
    @Autowired
    private GdnDtlMapper gdnDtlMapper;
    @Autowired
    private GdnDtlGoodsMapper gdnDtlGoodsMapper;

    public GdnHeaderMapper getGdnHeaderMapper() {
        return gdnHeaderMapper;
    }

    public GdnDtlMapper getGdnDtlMapper() {
        return gdnDtlMapper;
    }

    public GdnDtlGoodsMapper getGdnDtlGoodsMapper() {
        return gdnDtlGoodsMapper;
    }


    public List<OtherOutOrderInfoResponse> selectOtherTaskInfo(SearchOtherOutOrderRequest input) {
        return this.gdnManagerMapper.selectOtherTaskInfo(input);
    }

    public GdnHeader selectByNo(String billNo) {
        QueryWrapper<GdnHeader> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("GDN_NO", billNo).or().eq("SOURCE_SHIP_NOTICE", billNo);
        List<GdnHeader> list = this.gdnHeaderMapper.selectList(queryWrapper);
        if (list.size() == 0)
            return null;
        return list.get(0);
    }

    public List<GdnDtl> selectDetailsByHeaderId(Long id) {
        QueryWrapper<GdnDtl> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("GDN_HEADER_ID", id);
        return this.gdnDtlMapper.selectList(queryWrapper);
    }

    public List<OtherOutOrderGoodsResponse> selectGoodsByNo(SearchOrderGoodsRequest input) {
        return this.gdnDtlMapper.selectGoodsByNo(input);
    }

    public List<GdnDtl> selectDetailByIds(List<Long> ids) {
        QueryWrapper<GdnDtl> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("GDN_DTL_ID", ids);
        return this.gdnDtlMapper.selectList(queryWrapper);
    }

    public List<GdnDtlGoods> selectGoodsByDtlId(Long id) {
        QueryWrapper<GdnDtlGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("GDN_DTL_ID", id);
        return this.gdnDtlGoodsMapper.selectList(queryWrapper);
    }

    public boolean isDnGoods(String billNo, String sn) {
        int count = this.gdnManagerMapper.countPickTaskGoods(billNo, sn);
        return count > 0;
    }

    public List<String> selectNotExistPickTaskGoodsSn(String billNo, List<String> snList) {
        String str = String.join(",", snList);
        return this.gdnManagerMapper.selectNotExistPickTaskGoodsSn(billNo, str);
    }


    public List<GdnDtlGoods> selectPickTaskSn2dtlId(String gdnNo, List<String> snList) {
        return this.gdnManagerMapper.selectPickTaskSn2dtlId(gdnNo, snList);
    }
}
